package com.ecustbookstore.mymall.service.impl;

import com.ecustbookstore.mymall.common.CommonResponse;
import com.ecustbookstore.mymall.common.ResponseCode;
import com.ecustbookstore.mymall.mbg.mapper.PmsBrandMapper;
import com.ecustbookstore.mymall.mbg.pojo.PmsBrand;
import com.ecustbookstore.mymall.mbg.pojo.PmsBrandExample;
import com.ecustbookstore.mymall.service.PmsBrandService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @author: Tian
 * @time: 2021/6/1 23:25
 */

@Service("PmsBrandServiceImpl")
public class PmsBrandServiceImpl implements PmsBrandService {

    @Resource
    private PmsBrandMapper pmsBrandMapper;

    @Override
    public CommonResponse addBrand(PmsBrand pmsBrand) {
        //参数校验
        if (pmsBrand == null) {
            return CommonResponse.failed("请传入正确的品牌");
        }
        if (pmsBrandMapper.selectByPrimaryKey(pmsBrand.getId()) != null) {
            return CommonResponse.failed("该品牌已存在,添加品牌失败");
        }
        int count = pmsBrandMapper.insertSelective(pmsBrand);
        if (count == 1) {
            return CommonResponse.success("添加品牌成功");
        }
        return CommonResponse.failed("添加品牌失败");
    }

    @Override
    public CommonResponse deleteBrand(Long brandId) {
        int count = pmsBrandMapper.deleteByPrimaryKey(brandId);
        if (count == 0) {
            return CommonResponse.failed("该品牌不存在");
        } else {
            return CommonResponse.success("删除品牌成功");
        }
    }

    @Override
    public CommonResponse updateBrand(PmsBrand pmsBrand) {
        int count = pmsBrandMapper.updateByPrimaryKeySelective(pmsBrand);
        if (count == 0) {
            return CommonResponse.failed("更新品牌失败");
        } else {
            return CommonResponse.success("更新品牌成功");
        }
    }

    @Override
    public CommonResponse selectBrandById(Long id) {
        if (id == null) return CommonResponse.failed("参数错误");
        PmsBrand pmsBrand = pmsBrandMapper.selectByPrimaryKey(id);
        if (pmsBrand != null) {
            return CommonResponse.success("找到了", pmsBrand);
        }
        return CommonResponse.failed("查询失败");
    }

    @Override
    public CommonResponse<List<PmsBrand>> getList(String keyword, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PmsBrandExample pmsBrandExample = new PmsBrandExample();
        pmsBrandExample.setOrderByClause("sort desc");
        PmsBrandExample.Criteria criteria = pmsBrandExample.createCriteria();
        if (keyword != null) {
            criteria.andNameLike("%" + keyword + "%");
        }
        List<PmsBrand> pmsBrands = pmsBrandMapper.selectByExample(new PmsBrandExample());
        return CommonResponse.success(pmsBrands);
    }

    @Override
    public CommonResponse<List<PmsBrand>>  listAllBrand() {
        List<PmsBrand> pmsBrands = pmsBrandMapper.selectByExample(new PmsBrandExample());
        return CommonResponse.success(pmsBrands);
    }
}
